package com.csinc.generate.onekey;


import com.csinc.generate.core.utils.io.CopyDirUtils;
import com.csinc.generate.core.make.impl.freemarker.ApplicationBootMaker;
import com.csinc.generate.core.make.impl.freemarker.ApplicationPropertiesMaker;
import com.csinc.generate.core.make.impl.freemarker.LogbackMaker;
import com.csinc.generate.core.make.impl.freemarker.PomXmlMaker;
import com.csinc.generate.core.base.model.SettingBeanEntity;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Create By 郭金宝 on 2018/7/25.<br>
 */
public class BuildSpringBootProjectMaker {

    protected SettingBeanEntity data;
    protected String root;
    @Getter
    @Setter
    private String projectName = "default-project";

    public BuildSpringBootProjectMaker(SettingBeanEntity data) {
        this.data = data;
    }

    protected String createPath(String path,String pk) {
        String[] pks = pk.split("\\.");
        List<String> dirs = new ArrayList<>(Arrays.asList( getProjectName(), "src", "main", "java"));
        for (String p : pks) {
            dirs.add(p);
        }
        path = makePath(path, dirs);
        return path;
    }

    protected String createResourcePath() {
        List<String> dirs = new ArrayList<>(Arrays.asList(getProjectName(), "src", "main", "resources"));
        String path = makePath(root, dirs);
        return path;
    }
    protected String createResourcePath(String... resourceNames) {
        List<String> dirs = new ArrayList<>(Arrays.asList( getProjectName(), "src", "main"));
        for (String r : resourceNames) {
            dirs.add(r);
        }
        String path = makePath(root, dirs);
        return path;
    }

    protected String getProjectRootPackage(String pojoPackage) {
        int t = pojoPackage.lastIndexOf(".");
        return pojoPackage.substring(0, t);
    }

    protected void buildApplicationBootMaker() {
        String projectRootPackage = getProjectRootPackage(data.getPojoPackageProperty());
        String javaFilePath = createPath(root,projectRootPackage);
        createResourcePath();
        new ApplicationBootMaker()
                .projectRootPackage(projectRootPackage)
                .outPath(javaFilePath)
                .make();
    }

    protected void buildPomXmlMaker(String packagePath) {
        String projectRootPackage = getProjectRootPackage(packagePath);
        new PomXmlMaker()
                .projectRootPackage(projectRootPackage)
                .project(this.getProjectName())
                .outPath(root+this.getProjectName())
                .make();
    }


    protected void buildApplicationYmlMaker() {
        String projectRootPackage = getProjectRootPackage(data.getPojoPackageProperty());
        String resourcePath = createResourcePath("resources");
        new ApplicationPropertiesMaker()
                .projectRootPackage(projectRootPackage)
                .dBConnectionEntityData(data.getDbConnectionEntity())
                .dbName(data.getDbConnectionEntity().getNameProperty())
                .outPath(resourcePath)
                .make();
    }

    protected void buildLogbackMaker() {
        String projectRootPackage = getProjectRootPackage(data.getPojoPackageProperty());
        String resourcePath = createResourcePath();
        new LogbackMaker()
                .projectRootPackage(projectRootPackage)
                .logBackEntity(data.getLogBackEntity())
                .outPath(resourcePath)
                .make();
    }


    protected String makePath(String path, List<String> dirs) {
        return CopyDirUtils.makePath(path, dirs);
    }

}
